package b2.scene.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.widget.fight.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import org.flixel.*;

	public class TankEffect extends Sprite
	{
		//作用目标坦克
		public var targetTankId:int;
		//当前的所有状态（状态唯一号为索引, [持续时间, 施为坦克id, 作用值]作为元素
		public var effects:Object;
		//当前的状态图标
		private var effectIcons:Array;
		//当前的状态动画
		private var effectMovies:Array;
		//状态图标容器
		private var iconContainer:Sprite;
		//状态动画容器
		public var effectContainer:Sprite;
		
		public function TankEffect(targetTankId:int)
		{
			super();
			this.targetTankId = targetTankId; 
			this.effects = new Object();
			this.effectIcons = new Array();
			this.effectMovies = new Array();
			this.iconContainer = new Sprite();
			this.effectContainer = new Sprite();
			iconContainer.y = -45;
			addChild(iconContainer);
			effectContainer.x = PlayState.TILE_WIDTH / 2;
			effectContainer.y = PlayState.TILE_WIDTH / 2;
			addChild(effectContainer);
		}
		
		public function hasEffect(index:int):Boolean
		{
			return (effects != null && effects[index] != null);
		}
		
		public function addEffect(index:int, duration:int, conductTankId:int, value:int, value2:int):void
		{
			var state:PlayState = FlxG.state as PlayState;
			var targetTank:TankSprite = state.tanks[targetTankId];
			if (targetTank == null || targetTank.tweenLiteFlag)
				return;
			var oldEffectFlag:Boolean = hasEffect(index); 
			if (!oldEffectFlag)
			{			
				if (targetTank == null)
					return;
				var message:String = "";
				//增加攻击
				if (index == 1)
				{
					targetTank.attackPoint += value;
					message = "增加<font color='#FF0000'>攻击</font><font color='#00AA00'> " + value + " </font>点";
				}
				//增加暴击
				else if (index == 2)
				{
					targetTank.criticalAttackRatio += value;
					message = "增加<font color='#FF0000'>暴击</font><font color='#00AA00'> " + value + " </font>点";
				}
				//移动减速
				else if (index == 3)
				{
					value = targetTank.moveVelocity * value / 1000;
					targetTank.moveVelocity -= value;
					message = "减慢<font color='#FF0000'>移动</font><font color='#00AA00'> " + value + " </font>点";
				}
				//增加HP
				else if (index == 4)
				{
					message = "每秒增加<font color='#FF0000'>生命</font><font color='#00AA00'> " + value + " </font>点";
				}
				//攻击弱化
				else if (index == 5)
				{
					targetTank.attackPoint -= value;
					message = "减弱<font color='#FF0000'>攻击</font><font color='#00AA00'> " + value + " </font>点";
				}
				//防御弱化
				else if (index == 6)
				{
					targetTank.armPoint -= value;
					message = "减弱<font color='#FF0000'>防御</font><font color='#00AA00'> " + value + " </font>点";
				}
				//减少HP
				else if (index == 7)
				{
					message = "每秒减少<font color='#FF0000'>生命</font><font color='#00AA00'> " + value + " </font>点";
				}
				//减少EP
				else if (index == 8)
				{
					message = "每秒减少<font color='#FF0000'>能量</font><font color='#00AA00'> " + value + " </font>点";
				}
				//封印能量
				else if (index == 9)
				{
					message = "封印<font color='#FF0000'>能量</font>，无法使用特殊武器";
				}
				//时间停止，移动停止
				else if (index == 10 || index == 12 || index == EffectConfig.ASSAULT || index == EffectConfig.COLLIDE)
				{
					value = targetTank.moveVelocity;
					targetTank.moveVelocity -= value;
					if (index == 12)
						message = "停止<font color='#FF0000'>移动</font>";
					else if (index == 10)
						message = "<font color='#FF0000'>时间停止</font>，无法移动、发炮";
//					else if (index == EffectConfig.COLLIDE)
//						message = "<font color='#FF0000'>发生坦克撞击</font>，移动停滞";
				}
				//能量护盾
				else if (index == 11)
				{
					message = "开启<font color='#FF0000'>能量护盾</font>，吸收伤害<font color='#00AA00'> " + (value / 10).toString() + " </font>%";
				}
				//毒害
				else if (index == 13)
				{
					value = targetTank.moveVelocity * 800 / 1000;
					targetTank.moveVelocity -= value;
					targetTank.attackPoint -= value;
					message = "陷入<font color='#FF0000'>毒害</font>，移动速度减慢<font color='#00AA00'> 80 </font>%，攻击力减弱<font color='#00AA00'> 80 </font>%";
				}
				//封印炮弹
				else if (index == 14)
				{
					message = "封印<font color='#FF0000'>炮弹</font>，无法使用炮弹";
				}
				//增加EP
				else if (index == 15)
				{
					message = "每秒增加<font color='#FF0000'>能量</font><font color='#00AA00'> " + value + " </font>点";
				}
				//恐惧
				else if (index == 16)
				{
					message = "陷入<font color='#FF0000'>恐惧</font>，移动失控";
				}
				//荆棘
				else if (index == 17)
				{
					message = "开启<font color='#FF0000'>荆棘</font>，反弹伤害<font color='#00AA00'> " + value / 10 + " </font>%";
				}
				//攻击频率减慢
				else if (index == 18)
				{
					targetTank.fireCooldown += value;
					message = "增加<font color='#FF0000'>发炮间隔</font><font color='#00AA00'> " + (value / 1000).toFixed(1) + " </font>秒";
				}
				//移动加速
				else if (index == 20)
				{
					value = targetTank.moveVelocity * value / 1000;
					targetTank.moveVelocity += value;
					message = "加快<font color='#FF0000'>移动</font><font color='#00AA00'> " + value + " </font>点";
				}
				//头盔
				else if (index == EffectConfig.HELMET)
				{
					message = "开启<font color='#FF0000'>头盔</font>，抵挡来自正前方的炮弹";
				}
				//霜之哀伤
				else if (index == EffectConfig.FROST_MOURNE)
				{
					message = "开启<font color='#FF0000'>霜之哀伤</font>，增加<font color='#FF0000'>攻击</font><font color='#00AA00'> " + value + " </font>点"
						+ "同时每秒减少生命值<font color='#00AA00'> " + value2 + " </font>";
					targetTank.attackPoint += value;
				}
				else if (index == EffectConfig.BURN)
				{
					message = "开启<font color='#FF0000'>燃烧</font>，对碰撞到的敌军坦克形成伤害<font color='#00AA00'> " + value + " </font>";
				}
				else if (index == EffectConfig.NIRVANA)
				{
					value = targetTank.moveVelocity;
					targetTank.moveVelocity -= value;
					message = "进入<font color='#FF0000'>涅槃</font>，此状态下受到敌军攻击或者碰撞立即死亡";
				}
				if (message.length > 0)
				{
					message = "<font color='#0000FF'>" + targetTank.tankName + "</font><font color='#00AA00'> " + duration + " </font>秒内" + message;
					FightScene.getInstance().showBottomMessage(message);
				}
				effects[index] = [duration, conductTankId, value, value2];
				refresh();	
			}
			else
			{
				effects[index][0] = duration;
				effects[index][1] = conductTankId;
			}
		}
		
		public function removeEffect(index:int):void
		{
			var state:PlayState = FlxG.state as PlayState;
			var targetTank:TankSprite = state.tanks[targetTankId];
			if (targetTank == null || targetTank.tweenLiteFlag)
				return;
			if (hasEffect(index))
			{
				var value:int = effects[index][2];
				if (targetTank == null)
					return;
				//增加攻击
				if (index == 1)
				{
					targetTank.attackPoint -= value;
				}
				//增加暴击
				else if (index == 2)
				{
					targetTank.criticalAttackRatio -= value;
				}
				//移动减速
				else if (index == 3)
				{
					targetTank.moveVelocity += value;
				}
				//攻击弱化
				else if (index == 5)
				{
					targetTank.attackPoint += value;
				}
				//防御弱化
				else if (index == 6)
				{
					targetTank.armPoint += value;
				}
				//时间停止，移动停止
				else if (index == 10 || index == 12 || index == EffectConfig.ASSAULT || index == EffectConfig.COLLIDE)
				{
					targetTank.moveVelocity += value;
				}
				//毒害
				else if (index == 13)
				{
					targetTank.moveVelocity += value;
					targetTank.attackPoint += value;
				}
				//攻击频率减慢
				else if (index == 18)
				{
					targetTank.fireCooldown -= value;
				}
				//移动加速
				else if (index == 20)
				{
					targetTank.moveVelocity -= value;
				}
				//霜之哀伤
				else if (index == EffectConfig.FROST_MOURNE)
				{
					targetTank.attackPoint -= value;
				}
				//涅槃
				else if (index == EffectConfig.NIRVANA)
				{
					targetTank.moveVelocity += value;
					state.updateTankStatus(targetTankId, -1, 0, targetTank.healthPoint, 0, 0);
					state.updateTankStatus(targetTankId, -1, 0, 0, 0, targetTank.energyPoint);
				}
				delete effects[index];
				refresh();
			}
		}
		
		public function onTick(timer:FlxTimer):void
		{
			for (var indexStr:String in effects)
			{
				var index:int = parseInt(indexStr);
				var effect:Array = effects[index];
				var value:int = effect[2];
				var value2:int = effect[3];
				var state:PlayState = FlxG.state as PlayState;
				var targetTank:TankSprite = state.tanks[targetTankId];
				if (targetTank != null)
				{
					//加HP
					if (index == 4)
						state.updateTankStatus(targetTankId, -1, targetTank.currentHp, value, 0, 0);
					//减HP
					else if (index == 7)
						state.updateTankStatus(targetTankId, -1, targetTank.currentHp, -value, 0, 0);
					//减EP
					else if (index == 8)
						state.updateTankStatus(targetTankId, -1, 0, 0, targetTank.currentEp, -value);
					//加EP
					else if (index == 15)
						state.updateTankStatus(targetTankId, -1, 0, 0, targetTank.currentEp, value);
					//霜之哀伤（减HP）
					else if (index == EffectConfig.FROST_MOURNE)
						state.updateTankStatus(targetTankId, -1, targetTank.currentHp, -value2, 0, 0);
					//死亡凋零
					else if (index == EffectConfig.DEATH_DECAY)
					{
						var tanks:Object = (FlxG.state as PlayState).tanks;
						for (var tankIdStr:String in tanks)
						{
							var otherTank:TankSprite = tanks[tankIdStr];
							//给玩家造成伤害
							if (otherTank != null && otherTank.tankId > 0)
							{
								var inCircle:Boolean = false;
								var x:int = otherTank.x + PlayState.TILE_WIDTH / 2;
								var y:int = otherTank.y + PlayState.TILE_WIDTH / 2;
								if ((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y) < 4 * PlayState.TILE_WIDTH * PlayState.TILE_WIDTH)
									state.updateTankStatus(otherTank.tankId, -1, otherTank.currentHp, -value, 0, 0);
							}
						}
					}
				}
				if (--effect[0] <= 0)
					removeEffect(index);
			}
		}
		
		private function removeAll():void
		{
			if (effectIcons != null)
			{
				for (var i:int = 0; i < effectIcons.length; i++)
				{
					var effectIcon:Sprite = effectIcons[i];
					if (effectIcon != null && effectIcon.parent != null)
						effectIcon.parent.removeChild(effectIcon);
					delete effectIcons[i];
				}
				effectIcons = null;
			}
			if (effectMovies != null)
			{
				for (i = 0; i < effectMovies.length; i++)
				{
					var effectMovie:Sprite = effectMovies[i];
					if (effectMovie != null && effectMovie.parent != null)
						effectMovie.parent.removeChild(effectMovie);
					delete effectMovies[i];
				}
				effectMovies = null;
			}
		} 
		
		private function refresh():void
		{
			removeAll();
			effectIcons = new Array();
			effectMovies = new Array();
			for (var index:String in effects)
			{
				var effectConfig:EffectConfig = Config.effectConfigs[index];
				//图标
				var EffectIcon:Class = ResourceHelper.getInstance().getClassByName(effectConfig.iconImage);
				var effectIcon:Sprite = new EffectIcon();
				effectIcon.width = 20;
				effectIcon.height = 20;
				effectIcon.x = effectIcons.length * 22;
				iconContainer.addChild(effectIcon);
				iconContainer.x = 23 - iconContainer.width / 2;
				effectIcons.push(effectIcon);
				if (effectConfig.effectImage != "")
				{
					//效果动画
					var EffectImage:Class = ResourceHelper.getInstance().getClassByName(effectConfig.effectImage);
					var effectImage:MovieClip = new EffectImage();
					effectImage.width = effectConfig.imgWidth;
					effectImage.height = effectConfig.imgHeight;
					effectImage.x = effectConfig.imgPosX - PlayState.TILE_WIDTH / 2;
					effectImage.y = effectConfig.imgPosY - PlayState.TILE_WIDTH / 2;
					effectImage.alpha = effectConfig.imgAlpha / 1000;
					if (effectImage.totalFrames <= 1)
						effectImage.cacheAsBitmap = true;
					effectContainer.addChild(effectImage);
					effectMovies.push(effectImage);
				}
			}
		}
		
		//效果跟随坦克
		public function followTank(x:int, y:int, direction:int):void
		{
			this.x = x;
			this.y = y;
			effectContainer.rotation = 90 * direction; 
		}
	
		public function destroy():void
		{
			removeAll();
			effects = null;
		}
	}
}